﻿#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTimer>
#include <QDesktopWidget>
#include <QDebug>
#include "arrowwidget.h"
#include <iostream>
#include <QMenu>
using namespace std;
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setAutoFillBackground(false);  //这个不设置的话就背景变黑
    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground,true);
    QDesktopWidget *deskdop = QApplication::desktop();
    move((deskdop->width() - this->width())/2, (deskdop->height() - this->height())/2);
    //etScaledContents(True

    initLoadPixmap();
    startTimer(300);

    w1 = new ArrowWidget();

    w1->setText("miao miao miao miao miao miaovmiao miao miaomiao miao miaomiao miao miaomiao miao miao");
    cout << pos.x() << " " << pos.y();
    w1->myMove(this->geometry().x() + this->width() , this->geometry().y());
    //w1->adjustSize();
    w1->show();



    /*lable load image*/
    /*
    QImage image;
    QString path = "/home/mason/work/demo_code/Qt/demo_pet/1_1.png";
    image.load(path);
    ui->label->setPixmap(QPixmap::fromImage(image));
    */
}

void Widget::OnBscGroupRightAction()
{

}

void Widget::initLoadPixmap()
{
    for (int i = 0; i < 3; i++)
    {
        QString fileName=QString("/home/mason/work/demo_code/Qt/demo_pet/10_%1.png").arg(i + 1);
#ifdef Q_OS_WIN32
// win
        qDebug()<<"win32";
        fileName = QString(".\\img\\1_%1.png").arg(i + 1);

#endif
        qDebug()<<fileName;
        QPixmap map(fileName);
        pixmap_list.append(map);
    }
    pixmap_index = 0;

}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
        QRect q(0,0,360,360);
        QRect q2(20,20,360,360);

//        QImage image;
//        QString path = "/home/mason/work/demo_code/Qt/demo_pet/1_1.png";
//        image.load(path);
    pixmap_list[pixmap_index].scaled(this->width(),this->height(),Qt::KeepAspectRatio);
    painter.drawPixmap(this->rect(), pixmap_list.at(pixmap_index), pixmap_list.at(pixmap_index).rect());

}



void Widget::timerEvent(QTimerEvent *event)
{
    pixmap_index++;
    pixmap_index %= 3;
    repaint();  //chuang kou chong hui
}

void Widget::mousePressEvent(QMouseEvent *e)
{


//    w1->setText("miao miao miao");
//    w1->move(this->pos.x() + this->width(), this->pos.y() );
    //w1->show();
    //仅对鼠标左键响应，右键留着使用菜单功能
    if (e->button() == Qt::LeftButton)
        pos = e->globalPos();
    else {
        QMenu menu;
        //添加菜单项，指定图标、名称、响应函数
        menu.addAction(QIcon("E:\\work\\official_website\\html\\images\\arrows.png"), "",this,SLOT(OnBscGroupRightAction()));
        //在鼠标位置显示
        menu.exec(QCursor::pos());

    }
}
void Widget::mouseMoveEvent(QMouseEvent *e)
{
    //仅对鼠标左键响应，右键留着使用菜单功能
    if (!e->buttons().testFlag(Qt::LeftButton))
            return;
    int dx = e->globalX() - pos.x();
    int dy = e->globalY() - pos.y();
    pos = e->globalPos();
    move(x()+dx, y()+dy);
    qDebug()<< this->geometry().x() + this->width();

    w1->myMove(this->geometry().x() + this->width() , this->geometry().y());
#ifdef Q_OS_WIN32
    w1->move(this->geometry().x() + (this->width() / 2),\
             this->geometry().y() - (this->height()));
#endif
}


Widget::~Widget()
{
    delete ui;
}




